/**
 * @(#)SeachDataController.java 2017年6月2日
 * Copyright(C) 2017年6月2日 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;


import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.domian.ImsiRetentSh;
import org.jsbd.boss.domian.MiguDataSh;
import org.jsbd.boss.domian.TaskHandlerSh;
import org.jsbd.boss.service.ISearchDataService;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.SortList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 查询数据
 * @author <a href="mailto:zhouchao@zhexinit.com" >周超</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/searchData")
public class SearchDataController {
	public static final Logger LOGGER = LoggerFactory.getLogger(SearchDataController.class);

	@Autowired
	private ISearchDataService searchDataService;
	/**
	 * 咪咕账号执行列表页面
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "listModel")
	public String showHeartbeatApkConfigList(Model model) throws Exception {
		return "/searchData/miguDataList";
	}
	
	@RequestMapping(value = "imsiRetentModel")
	public String showImsiRetentModel(Model model) throws Exception {
		return "/searchData/imsiRetentList";
	}
	
	@RequestMapping(value = "retentTaskStatisticsModel")
	public String showRetentTaskModel(Model model) throws Exception {
		return "/searchData/retentTaskStatisticsList";
	}
	
	@RequestMapping(value = "retent1TaskStatisticsModel")
	public String showRetent1TaskModel(Model model) throws Exception {
		return "/searchData/retent1TaskStatisticsList";
	}
	
	@RequestMapping(value = "newTaskStatisticsModel")
	public String showNewTaskModel(Model model) throws Exception {
		return "/searchData/newTaskStatisticsList";
	}
	
	@RequestMapping(value = "newTaskStatisticsMonthModel")
	public String showNewTaskMonthModel(Model model) throws Exception {
		return "/searchData/newTaskStatisticsMonthList";
	}
	
	
	/**
	 * 
	 * 咪咕账号执行情况
	 * @author <a href="mailto:zhouchao@zhexinit.com" >周超</a>
	 * @param page
	 * @param ver
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/miguDataList")
	public List<MiguDataSh> miguDataList(MiguDataSh miguDataSh) throws Exception {
		if(StringUtils.isNotBlank(miguDataSh.getSearchDate())){
			miguDataSh.setSearchDate(miguDataSh.getSearchDate().replaceAll("-", ""));
		}
		return searchDataService.getMiguDataStatisticsList(miguDataSh);
	}
	
	@ResponseBody
	@RequestMapping("/getImsiRetentPageList")
	public Map<String,Object> getImsiRetentPageList(DataGridModel page,ImsiRetentSh imsiRetentSh) throws Exception {
		if(StringUtils.isNotBlank(imsiRetentSh.getCurrentDate())){
			imsiRetentSh.setCurrentDate(imsiRetentSh.getCurrentDate().replaceAll("-", ""));
		}
		
		return searchDataService.getImsiRetentPageList(page, imsiRetentSh);
	}
	
	@ResponseBody
	@RequestMapping("/getImsiRetentLogPageList")
	public Map<String,Object> getImsiRetentLogPageList(DataGridModel page,ImsiRetentSh imsiRetentSh) throws Exception {
		if(StringUtils.isNotBlank(imsiRetentSh.getCurrentDate())){
			imsiRetentSh.setCurrentDate(imsiRetentSh.getCurrentDate().replaceAll("-", ""));
		}
		return searchDataService.getImsiRetentLogPageList(page,imsiRetentSh);
	}

	
	@ResponseBody
	@RequestMapping("/getNewTaskPageList")
	public Map<String,Object> getNewTaskPageList(DataGridModel page,TaskHandlerSh taskHandlerSh) throws Exception {
		if(StringUtils.isBlank(taskHandlerSh.getStart())
				&&StringUtils.isBlank(taskHandlerSh.getEnd())){
			taskHandlerSh.setRd(DateUtil.getCurrentDateStr());
		}
		String appName = taskHandlerSh.getAppName();
		if (StringUtils.isNotBlank(appName)) {
			String[] appNames = appName.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : appNames) {
				keywords.add(keyword);
			}
			taskHandlerSh.setKeywords(keywords);
		}
		
		return searchDataService.getNewTaskPageList(page, taskHandlerSh);
	}
	
	@ResponseBody
	@RequestMapping("/getNewTaskPageMonthList")
	public Map<String,Object> getNewTaskPageMonthList(DataGridModel page,TaskHandlerSh taskHandlerSh) throws Exception {
		if (StringUtils.isBlank(taskHandlerSh.getStart()) && StringUtils.isBlank(taskHandlerSh.getEnd())) {
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
		    int month = cal.get(Calendar.MONTH) + 1;
		    if (month < 10) {
		    	taskHandlerSh.setStart(year + "-0" + month);
		    } else {
		    	taskHandlerSh.setStart(year + "-" + month);
		    }
		}
		String appName = taskHandlerSh.getAppName();
		if (StringUtils.isNotBlank(appName)) {
			String[] appNames = appName.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : appNames) {
				keywords.add(keyword);
			}
			taskHandlerSh.setKeywords(keywords);
		}
		return searchDataService.getNewTaskPageMonthList(page, taskHandlerSh);
	}
	
	@ResponseBody
	@RequestMapping("/getRetentTaskPageList")
	public Map<String,Object> getRetentTaskPageList(DataGridModel page,TaskHandlerSh taskHandlerSh) throws Exception {
		if(StringUtils.isBlank(taskHandlerSh.getStart())
				&&StringUtils.isBlank(taskHandlerSh.getEnd())){
			taskHandlerSh.setRd(DateUtil.getCurrentDateStr());
		}
		String appName = taskHandlerSh.getAppName();
		if (StringUtils.isNotBlank(appName)) {
			String[] appNames = appName.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : appNames) {
				keywords.add(keyword);
			}
			taskHandlerSh.setKeywords(keywords);
		}
		Map<String, Object> map = searchDataService.getRetentTaskPageList(page,taskHandlerSh);
		List<TaskHandlerSh> list = (List<TaskHandlerSh>) map.get("rows");
				
		/*List<String> marks = new ArrayList<String>();
		if (list != null && list.size() > 0) {
			for (TaskHandlerSh ths : list ) {
				marks.add(ths.getMark());
			}
		}
		
		String[] retents = retentTaskService.hasDoNum(marks);
		for (int i = 0; i < list.size(); i++) {
			String retNum = retents[i];
			TaskHandlerSh ths = list.get(i);
			Integer execute = Integer.parseInt(retNum.substring(retNum.indexOf("|") + 1));
			Integer totalNum = ths.getTotalNum();
			if (execute == 0 || totalNum == 0) {
				ths.setExecuteRate(0D);
			} else {
				ths.setExecuteRate(Double.valueOf(String.valueOf(NumUtil.divide(execute, ths.getTotalNum()))));
			}
			
			ths.setExecuteNum(execute);
		}*/
		if ("executeRate".equals(taskHandlerSh.getSort())) {
			SortList<TaskHandlerSh> sortList = new SortList<TaskHandlerSh>();  
			sortList.Sort(list, "getExecuteRate", taskHandlerSh.getOrder());  
		}
		
		return map;
	}
	
	//留存次数1次查询
	@ResponseBody
	@RequestMapping("/get1RetentTaskPageList")
	public Map<String,Object> get1RetentTaskPageList(DataGridModel page,TaskHandlerSh taskHandlerSh) throws Exception {
		if(StringUtils.isBlank(taskHandlerSh.getStart())
				&&StringUtils.isBlank(taskHandlerSh.getEnd())){
			taskHandlerSh.setRd(DateUtil.getCurrentDateStr());
		}
		String appName = taskHandlerSh.getAppName();
		if (StringUtils.isNotBlank(appName)) {
			String[] appNames = appName.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : appNames) {
				keywords.add(keyword);
			}
			taskHandlerSh.setKeywords(keywords);
		}
		Map<String, Object> map = searchDataService.getRetentTaskPageList(page,taskHandlerSh);
		List<TaskHandlerSh> list = (List<TaskHandlerSh>) map.get("rows");
		
		for (TaskHandlerSh sh : list) {
			if (sh.getExecute1Num() == null || sh.getTotal1Num() == null 
					|| sh.getTotal1Num() == 0 || sh.getExecute1Num() == 0) {
				sh.setExecuteRate(Double.valueOf("0"));
				continue;
			}
			BigDecimal b = new BigDecimal((float)sh.getExecute1Num()/sh.getTotal1Num()*100);
			sh.setExecuteRate(Double.parseDouble(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString()));
		}
		
		if ("executeRate".equals(taskHandlerSh.getSort())) {
			SortList<TaskHandlerSh> sortList = new SortList<TaskHandlerSh>();  
			sortList.Sort(list, "getExecuteRate", taskHandlerSh.getOrder());  
		}
		
		return map;
	}
 }
